<?php

class AjaxController extends Controller
{
	public function actionAction()
	{
		$this->render('action');
	}
    //处理地域的ajax动作
	public function actionDynamicCitys()
	{
		//选择省级初始化地区下拉框
		if(!empty($_GET['province'])){
			echo '<script>$(document).ready(function(){$("#MemAddres_zar_id").find("option").remove();
					$("#MemAddres_zar_id").append("<option>--请选择--</option>");});</script>';
		}
		//如果选择“请选择”，则初始化地区下拉框数据
		if(!empty($_GET['zar_parentid'])){
			$data=Area::model()->findAll('zar_parentid=:zar_parentid',
					array(':zar_parentid'=>(int) $_GET['zar_parentid']));
			//选择市级下拉框，初始化地区下拉框数据
			if(!empty($_GET['city']) && !$data){
				echo '<script>$(document).ready(function(){$("#MemAddres_zar_id").find("option").remove();
						$("#MemAddres_zar_id").append("<option>--请选择--</option>");});</script>';
			}
			$data=CHtml::listData($data,'zar_id','zar_title');	
			if(count($data)==0){
				$areamodel = Area::model()->findByPk((int) $_GET['zar_parentid']);
				echo CHtml::tag('option', array('value'=>$areamodel->zar_id),CHtml::encode($areamodel->zar_title),true);
				Yii::app()->end();
			}
			echo CHtml::tag('option', array('value'=>''),CHtml::encode("--请选择--"),true);
			foreach($data as $value=>$name)
			{
				echo CHtml::tag('option', array('value'=>$value),CHtml::encode($name),true);
			}
		}else{
				echo '<script>$(document).ready(function(){$("#MemAddres_zar_id").find("option").remove();
						$("#MemAddres_zar_id").append("<option>--请选择--</option>");});</script>';
				 if(!empty($_GET['province'])){	
				 	echo '<script>$(document).ready(function(){
				 			$("#city_id").append("<option>--请选择--</option>");});</script>';
				 	
				 }
		}
	}

	//异步发送邮件验证码，相关的发送、保存、验证邮件验证码函数定义在member模型
	public function actionSendEmailCaptcha(){
		$return = Member::model()->sendEmailCaptcha();
		echo $return ;
		Yii::app()->end();
	}
	/*产品详细页面，规格选择  */
	public function actionSpeSelect(){
		foreach($_GET['name'] as $key=>$val){
			if($_GET['index']==1){			   
				$zpa_val = md5($_GET['thisname'].','.$val);
			}else{			
			    $zpa_val = md5($val.','.$_GET['thisname']);
			}
			$zpa_id = ProAttribute::model()->findByAttributes(array('zp_id'=>$_GET['id'],'zps_md5'=>$zpa_val));
			if(!empty($zpa_id)){
				$zpa_id = ProAttribute::model()->findByAttributes(array('zp_id'=>$_GET['id'],'zpa_val'=>$val));
				if(!empty($zpa_id)){
				$zpa_ids []= $zpa_id->attributes['zpa_id'];
				}else{
				$zpa_ids []= '';
			 }
			}
		};
		print_r(json_encode($zpa_ids));
		exit;
	}
	
	
	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/

	//Ajax删除购物车商品
	public function actionAjaxDelCart()
	{
		$id = $_POST['rowid'];
      	$data = array(
            'rowid' => $id,
            'qty' => 0
        );
       if (Yii::app()->cart->update($data))
            $cart = Yii::app()->cart;
            $mycart = $cart->contents();
            //print_r($mycart);
			//$mycart = $this->render(array('_cart',array('model'=>$mycart));
            $this->renderPartial('//layouts/_cart');
           	//print_r($mycart);
	}

    //获取购物车商品数量
	public function actionAjaxCartNum()
	{
		$cart = Yii::app()->cart;
		$mycart = $cart->contents();
		//print_r($mycart);
		print_r($total = $cart->total_items());exit;		
	}

    //搜索提示词
    public function actionAjaxSearch()
    {
        $keyword = $_POST['keyword'];
        $criteria = new CDbCriteria;
        $criteria->select = 'zsl_keyword';
        $criteria->limit = 10;
        $criteria->condition="zsl_keyword like :name";
        $criteria->params=array(':name'=>'%'.$keyword.'%');
        $criteria->group = 'zsl_keyword';
        $tag = SearchLog::model()->findAll ($criteria);
        foreach($tag as $val) {
        	echo CHtml::tag('li', array('value'=>$val->zsl_keyword),CHtml::encode($val->zsl_keyword),true);
        }
    }

    //获取购物车商品数量
	public function actionSpePrice()
	{
		$data   = array();
		if($_POST['id']){
			$productid = $_POST['id'];
			$spes = rtrim($_POST['spe'],',');
			$spemd5 = md5($productid.$spes);
			$data['errors'] = 0;
		}else{
			$data['errors'] = 1;
		}
		//取规格组的库存与编号		
		$Attrs  = ProAttribute::model()->findByAttributes(array('zp_id'=>$productid,'zps_md5'=>$spemd5));
		//print_r($Attrs);		
		$data['inventory'] = $Attrs->zpa_stock;
		$data['goods_no'] = $Attrs->zpa_pro_no;
		//取规格组的价格		
		$speprice  = ProSpe::model()->findByAttributes(array('zp_id'=>$productid,'zpa_id'=>$Attrs->zpa_id));
		$data['speprice'] = $speprice->zps_price;
		print_r(json_encode($data));
		exit;
	}
    
	//收藏商品
	public function actionAjaxFavorites()
	{
		if($_POST['ajax'])
		{
			$has_fav = MemFav::model()->findByAttributes(array('zm_id'=>Yii::app()->user->userid, 'zp_id'=>$_POST['productid']));
			if(!$has_fav){
				$memfav=new MemFav;
				$memfav->zm_id=Yii::app()->user->userid; 
				$memfav->zp_id=$_POST['productid']; 
				$memfav->inputtime=time(); 
				$memfav->zmf_type=1;
				if($memfav->save()>0)
				{ 
	   				echo "商品收藏成功"; 
	   			}
   			}
   			else
   			{
   				 echo "您已经收藏了该商品，请勿重复收藏";
   			}
		}
	}

	//设置默认地址
	public function actionAjaxAddAdders()
	{
		if($_POST['zmaid']){
			MemAddres::model()->updateAll(array('zma_default'=>'0'), array('condition'=>'zm_id='.Yii::app()->user->getId()));
			$count = MemAddres::model()->updateByPk($_POST['zmaid'], array('zma_default'=>'1'));
				if($count>0)
				{ 
	   				echo "设置默认地址成功"; 
	   			}
   			}
	}

	//使用优惠券
	public function actionmyticket()
	{
		$ticketid = (int)Yii::app()->request->getParam('ticketid',0);
		$price = (float)Yii::app()->request->getParam('price',0);		
		$ticket    = MemTicket::model()->find('zmi_id=:ticketid And zm_id =:zm_id',array(':ticketid'=>$ticketid,':zm_id'=>Yii::app()->user->getId()));
		if(!empty($ticket))
		{
			$return['errors'] = '1';
			$return['ticket'] = $ticket->zmi_val;
			$totalprice = $price-$ticket->zmi_val;
			if( $totalprice > 0 ){
				$return['totalprice'] = $totalprice;
			}else{
				$return['totalprice'] = '0';
			}
		}else{
			$return['errors'] = '0';
			$return['msg'] = t('No Ticket');
		}		
		echo json_encode($return);
	}

	//使用满就送
	public function actionfullsent()
	{
		$price = (float)Yii::app()->request->getParam('price',0);		 
		$Fullsent = CmpPromotion::model()->getFullsent($price);
		if($Fullsent !=0 )
		{
			$return['errors'] = '1';
			$totalprice = $price-$Fullsent;
			if( $totalprice > 0 ){
				$return['totalprice'] = $totalprice;
			}else{
				$return['totalprice'] = '0';
			}
		}else{
			$return['errors'] = '0';
			$return['msg'] = t('No fullsent');
		}
		echo json_encode($return);
	}
	
	//验证收货地址是否超过10个
	public function actionAjaxAddAddress()
	{
		$model = MemAddres::model();
		$count = $model->count('zm_id=:mid',array(':mid'=>Yii::app()->user->userid));
		echo $count;
		
	}

}